package com.my.javaweb;

import com.my.frame.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(name = "VotePage" ,urlPatterns = "/vote")
public class VoteDemo extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//设置请求编码格式
        resp.setContentType("text/html;charset=utf-8");//设置响应编码格式

        ServletContext sc = this.getServletContext();

        //获取sesion中的数据
        User u = (User)req.getSession().getAttribute("user");
        //判断是否投过票了。
        if(req.getSession().getAttribute("vote")==null){
            //获取请求参数
            String idol = req.getParameter("king");
            //处理请求
            if(!idol.equals("")){
                if(idol.equals("1")){
                    if(sc.getAttribute("voteCommit1")!=null){
                        int count = (int)sc.getAttribute("voteCommit1");
                        count+=1;
                        sc.setAttribute("voteCommit1",count);
                    }else {
                        sc.setAttribute("voteCommit1",1);
                    }
                    resp.getWriter().write("投票成功！");
                }
                if(idol.equals("2")){
                    if(sc.getAttribute("voteCommit2")!=null){
                        int count = (int)sc.getAttribute("voteCommit2");
                        count+=1;
                        sc.setAttribute("voteCommit2",count);
                    }else {
                        sc.setAttribute("voteCommit2",1);
                    }
                    resp.getWriter().write("投票成功！");
                }
                if(idol.equals("3")){
                    if(sc.getAttribute("voteCommit3")!=null){
                        int count = (int)sc.getAttribute("voteCommit3");
                        count+=1;
                        sc.setAttribute("voteCommit3",count);
                    }else {
                        sc.setAttribute("voteCommit3",1);
                    }
                    resp.getWriter().write("投票成功！");
                }
                if(idol.equals("4")){
                    if(sc.getAttribute("voteCommit4")!=null){
                        int count = (int)sc.getAttribute("voteCommit4");
                        count+=1;
                        sc.setAttribute("voteCommit4",count);
                    }else {
                        sc.setAttribute("voteCommit4",1);
                    }
                    resp.getWriter().write("投票成功！");
                }
                if(idol.equals("5")){
                    if(sc.getAttribute("voteCommit5")!=null){
                        int count = (int)sc.getAttribute("voteCommit5");
                        count+=1;
                        sc.setAttribute("voteCommit5",count);
                    }else {
                        sc.setAttribute("voteCommit5",1);
                    }
                    resp.getWriter().write("投票成功！");
                }
                req.getSession().setAttribute("vote",1);//投票权用掉了
            }
            else
            {
                //重新投
                resp.sendRedirect("/Servlet/Vote.html");
                return;
            }

        }
        resp.getWriter().write("<html>");
        resp.getWriter().write("<head>");
        resp.getWriter().write("<body>");
        resp.getWriter().write("<form>");
        resp.getWriter().write("当前投票结果:<br/>");
        resp.getWriter().write("七号楼霸主橘大胖:"+sc.getAttribute("voteCommit1")+"<br/>");
        resp.getWriter().write("浪子狸:"+sc.getAttribute("voteCommit2")+"<br/>");
        resp.getWriter().write("粘人精三花妹妹:"+sc.getAttribute("voteCommit3")+"<br/>");
        resp.getWriter().write("猎户黑脸:"+sc.getAttribute("voteCommit4")+"<br/>");
        resp.getWriter().write("小弟最多白手套:"+sc.getAttribute("voteCommit5")+"<br/>");
        resp.getWriter().write("</form>");
        resp.getWriter().write("</body>");
        resp.getWriter().write("</head>");
        resp.getWriter().write("</html>");

    }
}
